package view;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.util.Iterator;
import java.util.List;
import javax.swing.GroupLayout;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import model.MediaButton;
import model.MultiMedia;
import noname.MultimediaControl;

public class MediaView extends javax.swing.JPanel {

    private MultimediaControl multimediaControl;
    private JPanel pnlNorth, pnlSouth;
    
    public MediaView(MultimediaControl multimediaControl) {
        this.multimediaControl = multimediaControl;
        initComponents();
        this.setVisible(true);
    }
    
    public void displayMultiMedia(List<MultiMedia> multiMediaItems) {
        
        if (multiMediaItems != null && multiMediaItems.size() > 0) {
            
            pnlNorth.removeAll();
            pnlSouth.removeAll();

            // Toon buttons voor ieder MultiMedia object
            Iterator<MultiMedia> itr = multiMediaItems.iterator();
            while (itr.hasNext()) {
                MultiMedia multiMedia = itr.next();

                MediaButton button = new MediaButton(multiMedia);
                button.setText(multiMedia.getName());
                button.addActionListener(multimediaControl);
                pnlNorth.add(button);
            }

            // Toon het eerste MultiMedia
            displayMultiMedia(multiMediaItems.get(0));

            revalidate();
            repaint();   
        }
    }
    
    public void displayMultiMedia(MultiMedia multiMedia) {
        pnlSouth.removeAll();
        JComponent component = multiMedia.getView();
        pnlSouth.add(component);
        
        revalidate();
        repaint();
    }
    
    private void initComponents() {
        
        // Panel met buttons
        pnlNorth = new JPanel();
        pnlNorth.setLayout(new FlowLayout(FlowLayout.LEFT));
        pnlNorth.setBorder(new LineBorder(Color.BLACK));
        
        // Panel met media weergave
        pnlSouth = new JPanel();
        pnlSouth.setLayout(new GridLayout(1, 1));
        pnlSouth.setBorder(new LineBorder(Color.BLACK));
        
        GroupLayout layout = new GroupLayout(this);
        setLayout(layout);
        
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        
        layout.setVerticalGroup(layout.createSequentialGroup()
                .addComponent(pnlNorth, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addComponent(pnlSouth));
        layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
                .addComponent(pnlNorth)
                .addComponent(pnlSouth));
    }
}
